<script src="/login/status"></script>
<script>
  if (!isLogin) {
    location.href = '/admin/login.html';
  }
</script>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Slides &laquo; Admin</title>
  <link rel="stylesheet" href="../assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="../assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="../assets/css/admin.css">
  <script src="../assets/vendors/nprogress/nprogress.js"></script>
</head>

<body>
  <script>NProgress.start()</script>

  <div class="main">
    <nav class="navbar">
      <button class="btn btn-default navbar-btn fa fa-bars"></button>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="profile.html"><i class="fa fa-user"></i>个人中心</a></li>
        <li><a href="javascript:;" id="logout"><i class="fa fa-sign-out"></i>退出</a></li>
      </ul>
    </nav>
    <div class="container-fluid">
      <div class="page-title">
        <h1>图片轮播</h1>
      </div>
      <!-- 有错误信息时展示 -->
      <!-- <div class="alert alert-danger">
        <strong>错误！</strong>发生XXX错误
      </div> -->
      <div class="row">
        <div class="col-md-4">
          <form>
            <h2>添加新轮播内容</h2>
            <div class="form-group">
              <label for="image">图片</label>
              <!-- show when image chose -->
              <img class="help-block thumbnail" style="display: none">
              <input id="image" class="form-control" type="file">
              <input type="hidden" name="image" id="hidden">
            </div>
            <div class="form-group">
              <label for="text">文本</label>
              <input id="text" class="form-control" name="title" type="text" placeholder="文本">
            </div>
            <div class="form-group">
              <label for="link">链接</label>
              <input id="link" class="form-control" name="link" type="text" placeholder="链接">
            </div>
            <div class="form-group">
              <!-- <button class="btn btn-primary" type="submit">添加</button> -->
              <button class="btn btn-primary" type="button" id="btn">添加</button>
            </div>
          </form>
        </div>
        <div class="col-md-8">
          <table class="table table-striped table-bordered table-hover">
            <thead>
              <tr>
                <th class="text-center">图片</th>
                <th>文本</th>
                <th>链接</th>
                <th class="text-center" width="100">操作</th>
              </tr>
            </thead>
            <tbody>
              <!-- <tr>
                <td class="text-center"><img class="slide" src="../uploads/slide_1.jpg"></td>
                <td>XIU功能演示</td>
                <td>#</td>
                <td class="text-center">
                  <a href="javascript:;" class="btn btn-danger btn-xs">删除</a>
                </td>
              </tr>
              <tr>
                <td class="text-center"><img class="slide" src="../uploads/slide_2.jpg"></td>
                <td>XIU功能演示</td>
                <td>#</td>
                <td class="text-center">
                  <a href="javascript:;" class="btn btn-danger btn-xs">删除</a>
                </td>
              </tr> -->
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <div class="aside">
    <div class="profile">
      <img class="avatar" src="../uploads/avatar.jpg">
      <h3 class="name">Coder</h3>
    </div>
    <ul class="nav">
      <li>
        <a href="index.html"><i class="fa fa-dashboard"></i>仪表盘</a>
      </li>
      <li>
        <a href="#menu-posts" class="collapsed" data-toggle="collapse">
          <i class="fa fa-thumb-tack"></i>文章<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-posts" class="collapse">
          <li><a href="posts.html">所有文章</a></li>
          <li><a href="post-add.html">写文章</a></li>
          <li><a href="categories.html">分类目录</a></li>
        </ul>
      </li>
      <li>
        <a href="comments.html"><i class="fa fa-comments"></i>评论</a>
      </li>
      <li>
        <a href="users.html"><i class="fa fa-users"></i>用户</a>
      </li>
      <li class="active">
        <a href="#menu-settings" data-toggle="collapse">
          <i class="fa fa-cogs"></i>设置<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-settings" class="collapse in">
          <li class="active"><a href="slides.html">图片轮播</a></li>
          <li><a href="settings.html">网站设置</a></li>
        </ul>
      </li>
    </ul>
  </div>

  <script src="../assets/vendors/jquery/jquery.js"></script>
  <script src="../assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script src="../assets/js/commont.js"></script>
  <script src="../assets/vendors/art-template/template-web.js"></script>
  <script type="text/html" id="sTpl">
    {{each data}}
      <tr>
        <td class="text-center"><img class="slide" src="{{$value.image}}"></td>
        <td>{{$value.title}}</td>
        <td>{{$value.link}}</td>
        <td class="text-center">
          <a href="javascript:;" class="btn btn-danger btn-xs del" data-id="{{@$value._id}}">删除</a>
        </td>
      </tr>
    {{/each}}
  </script>
  <script>
    // 定义一个数组 
    let slideArr = [];

    $.ajax({
      type: 'get',
      url: '/slides',
      success: function (res) {
        //  console.log(res);
        slideArr = res;
        render();
      }
    })


    function render() {
      let html = template('sTpl', { data: slideArr });
      $('tbody').html(html);
    }

    // 删除功能 
    $('tbody').on('click', '.del', function () {
      let id = $(this).attr("data-id");
      if (confirm("你真的删除吗?")) {
        $.ajax({
          type: 'delete',
          url: '/slides/' + id,
          success: function (res) {
            let index = slideArr.findIndex(item => item._id === res._id);
            slideArr.splice(index, 1);
            render();
          }
        })
      }
    })


    // 第一步：先完成图片上传的功能
    $('#image').on('change', function () {
      let formData = new FormData();
      formData.append('img', this.files[0]);
      $.ajax({
        type: 'post',
        url: '/upload',
        data: formData,
        processData: false,
        contentType: false,
        success: function (res) {
          // console.log(res);
          $('.thumbnail').show().attr('src', res[0].img);
          $('#hidden').val(res[0].img);
        }
      })
    })

    // 实现轮播图的添加功能 
    $('#btn').on('click', function () {
      let data = $('form').serialize();
      $.ajax({
        type: 'post',
        url: '/slides',
        data: data,
        success: function (res) {
          slideArr.push(res);
          render();
          // 把对应的数据清空
          $('.thumbnail').hide().attr('src','');
          $('#hidden').val('');
          $('input[name="title"]').val('');
          $('input[name="link"]').val('');
        }
      })
    })

  </script>
  <script>NProgress.done()</script>
</body>

</html>